package com.adminlab.vos
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.events.CollectionEvent;
	
	[Bindable]
	public class Report extends ReportSummary
	{
		public var delived:Date;
		private var _exams:ArrayCollection = new ArrayCollection();
		
		public function get exams():ArrayCollection
		{
			return _exams;
		}
		
		public function set exams( value:ArrayCollection ):void
		{
			if( _exams != value )
			{
				_exams = value;
				if( _exams )
				{
					/* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
					var reportSort:Sort = new Sort();
					reportSort.fields = null;
					reportSort.compareFunction = reportCompareFunction;
					/* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
					_exams.sort = reportSort;
					_exams.refresh();
					
					_exams.addEventListener(CollectionEvent.COLLECTION_CHANGE, onExamsChange, false, 0, true ); 
					
				}
			}
		}
		
		private function onExamsChange( event:Event ):void
		{
			_exams.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onExamsChange, false ); 
			_exams.refresh();
			_exams.addEventListener(CollectionEvent.COLLECTION_CHANGE, onExamsChange, false, 0, true ); 
		}

		
		[Transient]
		public var saved:Boolean = false;
		
		public function Report()
		{
		}

		public function updateExam( exam:Exam ):void
		{
			if( exams.getItemIndex( exam ) == -1 )
			{	
				exam.id = exams.length + 1;
				exams.addItem( exam );
			}
		}
	
		private function reportCompareFunction(objExam1:Object, objExam2:Object, fields:Array = null):int
		{
			var exam1:Exam = objExam1 as Exam;	
			var exam2:Exam = objExam2 as Exam;
			if( exam1.examType.order < exam2.examType.order )
			{
				return -1;
			}
			if( exam1.examType.order > exam2.examType.order )
			{
				return 1;
			}
			
			return 0;
		}

	
	}
}